<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/11/13
 * Time: 21:47
 */
namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use app\models\DwDataCity;
use yii\filters\VerbFilter;

class CityController extends Controller
{
    public $enableCsrfValidation=false;
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex()
    {
        if(isset(Yii::$app->request->queryParams['id'])){
            $cityId = Yii::$app->request->queryParams['id'];
            $model = DwDataCity::findOne($cityId);
            $sql = "select t2.job_name,t1.total,t1.talk,t1.avg,t1.date_line,t1.job_id,sum(t1.20k_30k+30K) 20k from dw_data_job_num_by_day t1 left JOIN dw_data_jobs t2 on t1.job_id = t2.id  where t1.date_line = (select max(date_line) from  t_data_city_list where city_id ='{$cityId}') and t1.city_id = '{$cityId}' and t1.total>'100' group by job_id ORDER BY avg desc
";
            $citybyJobsData = Yii::$app->db->createCommand($sql)->queryAll();

        }else{
            return $this->redirect(['site/index']);
        }
        return $this->render('index', [
            'model'=>$model,
            'citybyJobsData'=>$citybyJobsData
        ]);

    }

    public function actionAvgdesc()
    {
        //$sql = "select sum(total_num) total_num  from t_data_city_list where date_line = (select max(date_line) from  t_data_city_list)";

        //$jobsNum = Yii::$app->db->createCommand($sql)->queryScalar();
        return $this->render('avgdesc', [
            //'jobsNum'=>$jobsNum
        ]);
    }

    /**
     * 平均工资一周变化情况
     */
    public function actionAvgchangeajax(){
        if(isset(Yii::$app->request->queryParams['id'])){
            $cityId = Yii::$app->request->queryParams['id'];
            $sql = "select t2.city_name,t1.* from t_data_city_list t1 left JOIN dw_data_city t2 on t1.city_id = t2.id  where t1.city_id = '{$cityId}' ORDER BY t1.date_line desc limit 7";
            $cityData = Yii::$app->db->createCommand($sql)->queryAll();
            krsort($cityData);
            $retrunData =array();
            foreach($cityData as $row){
                $retrunData[]= $row;
            }
            $retrunData = json_encode($retrunData);
            echo $retrunData;
        }else{
            echo '';
        }
    }

    public function actionAvgdescajax(){

        $sql = "select t2.city_name,t1.* from t_data_city_list t1 left JOIN dw_data_city t2 on t1.city_id = t2.id  where date_line = (select max(date_line) from  t_data_city_list) and total_num > '10000'  order by avg desc limit 20 ";

        $jobsData = Yii::$app->db->createCommand($sql)->queryAll();
        //var_dump($jobsData);
        krsort($jobsData);
        $retrunData =array();
        foreach($jobsData as $row){
            $retrunData[]= $row;
        }
        //var_dump($jobsData);
        $retrunData = json_encode($retrunData);
        echo $retrunData;
    }

}
